package com.windystory.app.antdesign;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.bluetooth.BluetoothGatt;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.clj.fastble.callback.BleGattCallback;
import com.clj.fastble.callback.BleNotifyCallback;
import com.clj.fastble.callback.BleScanCallback;
import com.clj.fastble.data.BleDevice;
import com.clj.fastble.exception.BleException;
import com.clj.fastble.utils.HexUtil;
import com.windystory.wsbluetoothlibrary.BluetoothManager;
import com.windystory.wslibrary.base.activity.BaseActivity;
import com.windystory.wslibrary.util.LogUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class BlueToothActivity extends BaseActivity {

    private BluetoothManager bluetoothManager;
    private Button start;
    private List<BleDevice> bleDeviceList = new ArrayList<>();
    private RecyclerView bluelist;
    private ItemAdaper adaper;


    @Override
    public int getLayoutRes() {
        return R.layout.activity_blue_tooth;
    }

    @Override
    protected boolean setEnableSwipeBack() {
        return false;
    }

    @Override
    protected void initView() {
        bluelist = (RecyclerView) findViewById(R.id.list);
        start = (Button) findViewById(R.id.start);
    }

    @Override
    protected void initData() {
        adaper = new ItemAdaper(bleDeviceList);
        bluelist.setLayoutManager(new LinearLayoutManager(this));
        bluelist.setAdapter(adaper);
        bluetoothManager = new BluetoothManager(this);
        bluetoothManager.initBluetooth(true, 1, 1000 * 5, 20, 1000 * 10, 10 * 1000);
    }

    @Override
    protected void initListener() {

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bleDeviceList.clear();
                adaper.notifyDataSetChanged();
                bluetoothManager.scan(new BleScanCallback() {

                    @Override
                    public void onScanStarted(boolean success) {

                    }

                    @Override
                    public void onScanning(BleDevice bleDevice) {
                        addDevice(bleDevice);
                        adaper.notifyDataSetChanged();
                    }

                    @Override
                    public void onScanFinished(List<BleDevice> bleDeviceList) {


//                for (int i = 0; i < bleDeviceList.size(); i++) {
//                    String data = HexUtil.encodeHexStr(bleDeviceList.get(i).getScanRecord());
//                    tips.append(
//                            ("蓝牙信息" +
//                                    "key:" + bleDeviceList.get(i).getKey() + " " +
//                                    "mac:" + bleDeviceList.get(i).getMac() + " " +
//                                    "name:" + bleDeviceList.get(i).getName() + " " +
//                                    "device:" + bleDeviceList.get(i).getDevice() + " " +
//                                    "rssi:" + bleDeviceList.get(i).getRssi() + " " +
//                                    "timestamp:" + bleDeviceList.get(i).getTimestampNanos() + " " +
//                                    "byte[]:" + bleDeviceList.get(i).getScanRecord() + " " +
//                                    "--->转换后的数据--》major-->" + bluetoothManager.getMajorInfo(data) + " minor-->" + bluetoothManager.getMinorInfo(data) + " UUID-->" + bluetoothManager.getUUID(data) + "\n"
//                            )
//                    );
//                }
                    }
                });
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bluetoothManager != null)
            bluetoothManager.cancelScan();
    }


    public class ItemAdaper extends BaseQuickAdapter<BleDevice, BaseViewHolder> {

        public ItemAdaper(@Nullable List<BleDevice> data) {
            super(android.R.layout.simple_list_item_1, data);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder helper, BleDevice item) {
            helper.setText(android.R.id.text1, item.getName() + "---" + item.getMac());


            helper.getView(android.R.id.text1).setOnClickListener(view -> {
                bluetoothManager.connect(item, new BleGattCallback() {
                    @Override
                    public void onStartConnect() {

                    }

                    @Override
                    public void onConnectFail(BleDevice bleDevice, BleException exception) {

                    }

                    @Override
                    public void onConnectSuccess(BleDevice bleDevice, BluetoothGatt gatt, int status) {
                        Toast.makeText(BlueToothActivity.this, "连接成功", Toast.LENGTH_SHORT).show();
                        bluetoothManager.notify(bleDevice, bleDevice.getKey(), "1", true, new BleNotifyCallback() {
                            @Override
                            public void onNotifySuccess() {

                            }

                            @Override
                            public void onNotifyFailure(BleException exception) {

                            }

                            @Override
                            public void onCharacteristicChanged(byte[] data) {
                                LogUtil.e(new String(data) + "-----");
                            }
                        });
                    }

                    @Override
                    public void onDisConnected(boolean isActiveDisConnected, BleDevice device, BluetoothGatt gatt, int status) {

                    }
                });
            });
        }
    }


    public void addDevice(BleDevice bleDevice) {
        removeDevice(bleDevice);
        bleDeviceList.add(bleDevice);
    }

    public void removeDevice(BleDevice bleDevice) {
        for (int i = 0; i < bleDeviceList.size(); i++) {
            BleDevice device = bleDeviceList.get(i);
            if (bleDevice.getKey().equals(device.getKey())) {
                bleDeviceList.remove(i);
            }
        }
    }

}
